package com.projectzqjz.parttimework.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.luozm.captcha.Captcha;
import com.projectzqjz.parttimework.App;
import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.base.BaseActivity;
import com.projectzqjz.parttimework.entity.PartBaseEntity;
import com.projectzqjz.parttimework.net.MyCallback;
import com.projectzqjz.parttimework.net.NoValidationTask;
import com.projectzqjz.parttimework.utils.DataCheckUtils;
import com.projectzqjz.parttimework.utils.NewsToastUtils;
import com.projectzqjz.parttimework.utils.ScreenUtil;
import com.projectzqjz.parttimework.utils.TitleBuilder;
import com.projectzqjz.parttimework.widget.CenterDialog;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class PartLoginActivity extends BaseActivity {
    @BindView(R.id.ed_phone)
    EditText ed_phone;
    private String codeId;


    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_part_login;
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void initView() {
        ed_phone.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)}); //限制输入位数
        new TitleBuilder(this).setTitleText("")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @OnClick({R.id.tv_share})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        //发送验证码
        if (view.getId() == R.id.tv_share) {
            if (!DataCheckUtils.isCellphone(ed_phone.getText().toString().trim())) {
                NewsToastUtils.showToast(PartLoginActivity.this, "请输入正确的手机号！");
            } else {
                //发送验证码
                sendVerify();
            }
        }
    }

    //发送短信
    private void sendVerify() {
        Dialog dialog = new CenterDialog(R.layout.loading_layout, PartLoginActivity.this).showLoad("正在获取验证码...");
        NoValidationTask.getApiService().sendCode(ed_phone.getText().toString().trim()).enqueue(new MyCallback<PartBaseEntity>(PartLoginActivity.this) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                dialog.dismiss();
                assert response.body() != null;
                String jsonStr = JSONObject.toJSONString(response.body().getData());
                JSONObject jsonObject = JSON.parseObject(jsonStr);
                codeId = jsonObject.getString("codeid");
                NewsToastUtils.showToast(App.getContext(), response.body().getMsg());
                startActivity(new Intent(PartLoginActivity.this, PartSendCodeActivity.class).putExtra("phone", ed_phone.getText().toString().trim()).putExtra("codeId", codeId));
                finish();
            }

            @Override
            protected void onFailure(Throwable t) {
                dialog.dismiss();
                NewsToastUtils.showToast(App.getContext(), "验证码获取失败！");
            }
        });
    }

    @Override
    public void initData() {

    }
}
